<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Inviter
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 * @version    $Id: ReferralsController.php 2010-07-02 19:54 mirlan $
 * @author     Mirlan
 */

/**
 * @category   Application_Extensions
 * @package    Inviter
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 */

class Inviter_ReferralsController extends Core_Controller_Action_Standard {
	public function indexAction() {
		if (! $this->_helper->api ()->user ()->getViewer ()->getIdentity ()) {
			$this->_redirect ( 'inviter' );
		}
		
		$this->view->navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'inviter_profile', array (), 'inviter_profile_referral' );
		$user = Engine_Api::_ ()->user ()->getViewer ();
		
		$inviterTable = $this->_helper->api ()->getDbtable ( 'invites', 'inviter' );
		$inviterSelect = $inviterTable->select ( 'new_user_id' )->where ( 'user_id = ? && new_user_id != 0', $user->user_id );
		$user_ids = array ();
		foreach ( $inviterTable->fetchAll ( $inviterSelect ) as $inviter ) {
			$user_ids [] = $inviter->new_user_id;
		}
		if (count ( $user_ids ) == 0)
			$user_ids = 0;
		else
			$user_ids = implode ( ', ', $user_ids );
		
		// Make paginator
		$table = Engine_Api::_ ()->getItemTable ( 'user' );
		$values ['select'] = $table->select ()->where ( "user_id IN ($user_ids)" )->order ( 'creation_date Desc' );
		$values ['page'] = $this->_getParam ( 'page', 1 );
		$values ['limit'] = 10;
		
		$this->view->paginator = $paginator = Engine_Api::_ ()->inviter ()->getInviterPaginator ( $values );
		$this->view->paginator = $paginator->setCurrentPageNumber ( $values ['page'] );
		
		// Suggest integration
		$coreModulesTbl = Engine_Api::_ ()->getDbTable ( 'modules', 'core' );
		$coreModuleSelect = $coreModulesTbl->select ()->where ( 'name = ?', 'suggest' )->where ( 'enabled = ?', 1 );
		
		$this->view->suggest_enabled = false;
		if ($coreModulesTbl->fetchRow ( $coreModuleSelect )) {
			$this->view->suggest_enabled = true;
		}
	}
}